package com.tanovo.service.impl;

import com.tanovo.bo.RoleFindBo;
import com.tanovo.bo.RoleResourceBindBo;
import com.tanovo.bo.TokenRequest;
import com.tanovo.bo.UserCore;
import com.tanovo.config.UserInfoContext;
import com.tanovo.constant.ResourcesConstant;
import com.tanovo.domain.*;
import com.tanovo.mapper.RoleMapper;
import com.tanovo.mapper.RoleResourceMapper;
import com.tanovo.mapper.UserRorleMapper;
import com.tanovo.service.IOauthClientDetailsService;
import com.tanovo.service.IResourcesService;
import com.tanovo.service.IRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;
    @Autowired
    private UserRorleMapper userRorleMapper;
    @Autowired
    private IResourcesService iResourcesService;
    @Autowired
    private  IOauthClientDetailsService iOauthClientDetailsService;
    @Autowired
    private   IRoleService iRoleService;
    @Override
    public List<Role> getRoleListByUserCore(UserCore userCore) {
        List<Role> roleList = roleMapper.getRoleListByUserCore(userCore);
        return roleList;
    }

    @Override
    public void addRole(Role role) {
        role.setDelFlag(0);
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());

        roleMapper.insert(role);
    }

    @Override
    public void delete(Integer roleId) {
        RoleExample example = new RoleExample();
        example.createCriteria().andIdEqualTo(roleId)
                .andClientIdEqualTo(UserInfoContext.getUser().getClientId());
        Role role = new Role();
        role.setId(roleId);
        role.setDelFlag(1);
        role.setUpdateTime(new Date());
        role.setUpdateBy(UserInfoContext.getUser().getId().toString());
        roleMapper.updateByExampleSelective(role, example);
    }

    @Override
    public void editRole(Role role) {
        RoleExample example = new RoleExample();
        example.createCriteria().andIdEqualTo(role.getId())
                .andClientIdEqualTo(UserInfoContext.getUser().getClientId());
        role.setUpdateTime(new Date());
        role.setUpdateBy(UserInfoContext.getUser().getId().toString());
        roleMapper.updateByExampleSelective(role, example);
    }

    @Override
    public List<Role> getRolelist(RoleFindBo roleFindBo) {
        List<Role> roles= roleMapper.getRolelist(roleFindBo);
        return roles;
    }

    @Override
    @Transactional
    public void bindResources(RoleResourceBindBo roleResourceBindBo) {
        List<Integer> bindResourceList = roleResourceBindBo.getBindResourceList();
        if (!CollectionUtils.isEmpty(bindResourceList)) {
            for (Integer resourceId : bindResourceList) {
                RoleResource roleResource = new RoleResource();
                roleResource.setRoleId(roleResourceBindBo.getRoleId());
                roleResource.setResourceId(resourceId);
                roleResource.setDelFlag(0);
                roleResource.setCreateBy(UserInfoContext.getUser().getId().toString());
                roleResource.setUpdateBy(UserInfoContext.getUser().getId().toString());
                Date date = new Date();
                roleResource.setCreateTime(date);
                roleResource.setUpdateTime(date);
                roleResource.setClientId(UserInfoContext.getUser().getClientId());
                roleResource.setCompanyId(UserInfoContext.getUser().getCompanyId());
                roleResourceMapper.insert(roleResource);
            }
        }
        this.unBindResources(roleResourceBindBo);
    }

    @Override
    public void unBindResources(RoleResourceBindBo roleResourceBindBo) {
        List<Integer> unBindResourceList = roleResourceBindBo.getUnBindResourceList();
        if (!CollectionUtils.isEmpty(unBindResourceList)) {
            RoleResourceExample example = new RoleResourceExample();
            example.createCriteria().andDelFlagEqualTo(0)
                    .andRoleIdEqualTo(roleResourceBindBo.getRoleId())
                    .andResourceIdIn(unBindResourceList)
                    .andClientIdEqualTo(UserInfoContext.getUser().getClientId())
                    .andCompanyIdEqualTo(UserInfoContext.getUser().getCompanyId());
            List<RoleResource> list = roleResourceMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(list)) {
                for (RoleResource roleResource : list) {
                    roleResource.setDelFlag(1);
                    roleResource.setUpdateTime(new Date());
                    roleResource.setUpdateBy(UserInfoContext.getUser().getId().toString());
                    roleResourceMapper.updateByPrimaryKeySelective(roleResource);
                }
            }
        }
    }

    @Override
    public List<Role> getCheckedRolelist(RoleFindBo roleFindBo) {
        List<Role> rolelist = this.getRolelist(roleFindBo);
        UserCore userCore = new UserCore();
        userCore.setId(roleFindBo.getUserId());
        userCore.setClientId(UserInfoContext.getUser().getClientId());
        if (!CollectionUtils.isEmpty(rolelist)) {
            List<Role> userRoleList = this.getRoleListByUserCore(userCore);
            if (!CollectionUtils.isEmpty(userRoleList)) {
                for (Role userRole : userRoleList) {
                    for (Role role : rolelist) {
                        if (userRole.getId().intValue() == role.getId().intValue()) {
                            role.setChecked(true);
                        }
                    }
                }
            }
        }

        return rolelist;
    }

    @Override
    public void deleteRoleList(List<Integer> roleIdList) {
        for (Integer roleId : roleIdList) {
            this.delete(roleId);
        }
    }



    @Override
    public void initAdmin( UserCore userCore) {
        TokenRequest request=new TokenRequest();
        request.setClientId(userCore.getClientId());
        OauthClientDetails oauthClientDetails = iOauthClientDetailsService.getOauthClientDetails(request);
        Role admin=new Role();
        admin.setCompanyId(userCore.getCompanyId());
        admin.setClientId(userCore.getClientId());
        admin.setName(oauthClientDetails.getAdditionalInformation()+"系统管理员");
        admin.setCode("admin");
        iRoleService.addRole(admin);
        //创建当前系统管理员角色 并赋值初始化权限.
        iResourcesService.giveAdminResources(admin);
    }
}
